package controller;

import com.github.pagehelper.PageInfo;
import domain.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import service.PermissionService;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Permission> all = permissionService.findAll(pageNum, pageSize);
        modelAndView.getModel().put("page",all);
        modelAndView.setViewName("permission_all");
        return modelAndView;
    }

    //新增权限
    @RequestMapping("/add")
    public String add(Permission permission){
        permissionService.addPermission(permission);
        return "redirect:/permission/all";
    }

    //权限详情
    @RequestMapping("/desc")
    public ModelAndView desc(int id){
        ModelAndView modelAndView = new ModelAndView();
        Permission permissionById = permissionService.findPermissionById(id);
        modelAndView.getModel().put("permission",permissionById);
        modelAndView.setViewName("permission_desc");
        return modelAndView;
    }

    //编辑前查询
    @RequestMapping("/information")
    public ModelAndView information(int id){
        ModelAndView modelAndView = new ModelAndView();
        Permission permissionById = permissionService.findPermissionById(id);
        modelAndView.getModel().put("permission",permissionById);
        modelAndView.setViewName("permission_update");
        return modelAndView;
    }

    //
    @RequestMapping("/update")
    public String update(Permission permission){
        permissionService.updatePermission(permission);
        return "redierct:/permission/all";
    }
}
